/*
 * CoverFlowNodes.fx
 *
 * Created on Mar 29, 2010, 2:25:56 PM
 */

package fxgreader.components;
import javafx.util.Math;
import javafx.scene.CustomNode;
import javafx.scene.Node;
import javafx.scene.Group;
import fxgreader.components.CoverFlowNode;
import java.util.ArrayList;

/**
 * @author perkintang
 */

public class CoverFlow extends CustomNode{
public var nodes : CoverFlowNode[];
public var nodeList : ArrayList = new ArrayList(100);
public var nodeW : Number;
public var nodeH : Number;
public var lazyLoad : function():Void;
public var onClick : function(node : CoverFlowNode):Void;
var scalrate : Number = 1.5;
var xoffset:Number = 50;
var yoffset:Number = 90;
var dw = 20;
var duh = -20;
var dlh = -15;
public var centerIndex : Integer = 0;
var overset = 60;
public function newNodesAdded(){
    println("display new nodes : centerIndex at {centerIndex} and nodeList size is{nodeList.size()}");
    var tmp : CoverFlowNode ;
    for(x in [0 .. nodeList.size()-1]){
            tmp = nodeList.get(x) as CoverFlowNode;
            if(x > centerIndex){
                tmp.offsetx_to = (nodeW*scalrate - overset) + xoffset*Math.abs(x-centerIndex) ;
                tmp.ori_width_to = nodeW;
                tmp.ori_height_to = nodeH;
                tmp.offsety_to = yoffset;
                tmp.duh_to =duh;
                tmp.dlh_to = dlh;
                tmp.dw_to = dw;
                tmp.quickMove(false);
                tmp.scalrate_to = 1;
                tmp.toBack();
                if((x - centerIndex ) <= 2){
                        tmp.load();
                }
                if((x - centerIndex) > 2){
                        tmp.release();
                }
            }
    }
}
function clickat(index : Integer):Void{
    var clickedNode = nodeList.get(index) as CoverFlowNode;
    var crossedIndex = 0;
    if(centerIndex >= nodeList.size()){
            crossedIndex = nodeList.size() - index;
    }else{
            crossedIndex = Math.abs(centerIndex - index);
            if(nodeList.size() - centerIndex < 5){
                    lazyLoad();
            }
    }
    clickedNode.dw_to = 0;
    clickedNode.duh_to = 0;
    clickedNode.dlh_to = 0 ;
    clickedNode.ori_height_to = nodeH*scalrate ;
    clickedNode.ori_width_to = nodeW*scalrate ;
    clickedNode.offsetx_to = 0;
    clickedNode.offsety_to = yoffset-65;
    clickedNode.scalrate_to = scalrate;
    clickedNode.move();
    clickedNode.load();
    onClick(clickedNode);
    var tmp : CoverFlowNode ;
    if(index < centerIndex){
        var fromindex = index -5;
        var toindex = index + 5;
        if(fromindex < 0){
            fromindex =0;
        }
        if(toindex >= nodeList.size()){
            toindex = nodeList.size() -1;
        }
        for(x in [0 .. fromindex-1]){
              tmp = nodeList.get(x) as CoverFlowNode;
              tmp.release();
              tmp.hidden();
        }
        for(x in [toindex+1 .. nodeList.size() -1]){
              tmp = nodeList.get(x) as CoverFlowNode;
              tmp.release();
              tmp.hidden();
        }
        for(x in [fromindex .. toindex]){
            tmp = nodeList.get(x) as CoverFlowNode;
            if(x < index){
                tmp.offsetx_to = (overset-nodeW) - Math.abs(x-index)*xoffset;
                tmp.move();
                tmp.toFront();
                if((index -x ) <= 2){
                        tmp.load();
                }
            }
            if(x > index){
                tmp.offsetx_to = (nodeW*scalrate - overset) + xoffset*Math.abs(x-index) ;
                tmp.ori_width_to = nodeW;
                tmp.ori_height_to = nodeH;
                tmp.offsety_to = yoffset;
                tmp.duh_to =duh;
                tmp.dlh_to = dlh;
                tmp.dw_to = dw;
                tmp.move();
                tmp.scalrate_to = 1;
                tmp.toBack();
                if((x - index) > 2){
                        tmp.release();
                }
            }
        }
    }
    if(index > centerIndex){
         var fromindex = index -5;
         var toindex = index + 5;
         if(fromindex < 0){
             fromindex =0;
         }
         if(toindex >= nodeList.size()){
             toindex = nodeList.size() -1;
         }
         for(x in [0 .. fromindex-1]){
              tmp = nodeList.get(x) as CoverFlowNode;
              tmp.release();
              tmp.hidden();
         }
         for(x in [toindex+1 .. nodeList.size() -1]){
              tmp = nodeList.get(x) as CoverFlowNode;
              tmp.release();
              tmp.hidden();
         }
         for(x in [fromindex .. toindex]){
            tmp = nodeList.get(x) as CoverFlowNode;
            if(x < index){
                tmp.offsetx_to = (overset-nodeW) - Math.abs(x-index)*xoffset;
                tmp.ori_width_to = nodeW;
                tmp.ori_height_to = nodeH;
                tmp.offsety_to = yoffset;
                tmp.duh_to = -duh;
                tmp.dlh_to = -dlh;
                tmp.dw_to = dw;
                tmp.move();
                tmp.scalrate_to = 1;
                tmp.toFront();
                if((index -x ) > 2){
                        tmp.release();
                }
            }
            if(x > index){
                tmp.offsetx_to = (nodeW*scalrate - overset) + xoffset*Math.abs(x-index) ;
                tmp.move();
                tmp.toBack();
                if((x - index) <= 2){
                        tmp.load();
                }
            }
        }
    }
    centerIndex = index;
    clickedNode.toFront();
};
var group : Group;
override function create():Node {
        group = Group{
            content : nodes
        };
        //set first
        nodes[0].indexOfpositionerArray = 0;
        nodes[0].ori_height = nodeH*scalrate;
        nodes[0].ori_width = nodeW*scalrate;
        nodes[0].offsetX = 0;
        nodes[0].offsetY = yoffset - 65;
        nodes[0].dw = 0;
        nodes[0].dlh = 0;
        nodes[0].duh = 0;
        nodes[0].ori_height_to = nodeH*scalrate;
        nodes[0].ori_width_to = nodeW*scalrate;
        nodes[0].offsetx_to = 0;
        nodes[0].offsety_to = yoffset - 65;
        nodes[0].dw_to = 0;
        nodes[0].dlh_to = 0;
        nodes[0].duh_to = 0;
        nodes[0].scalrate = scalrate;
//        nodes[0].image = Image { url: "{__DIR__}img{0+1}.jpg" };
        nodes[0].onclick = clickat;
        nodes[0].toBack();
        nodes[0].load();
        nodeList.add(nodes[0]);
        for(index in [1 .. sizeof nodes -1]){
            nodes[index].indexOfpositionerArray = index;
            nodes[index].ori_height = nodeH;
            nodes[index].ori_width = nodeW;
            nodes[index].offsetX = nodeW*scalrate + xoffset*index - overset;
            nodes[index].offsetY = yoffset;
            nodes[index].dw = dw;
            nodes[index].dlh = dlh;
            nodes[index].duh = duh;
            nodes[index].ori_height_to = nodeH;
            nodes[index].ori_width_to = nodeW;
            nodes[index].offsetx_to = xoffset*index;
            nodes[index].offsety_to = yoffset;
            nodes[index].dw_to = dw;
            nodes[index].dlh_to = dlh;
            nodes[index].duh_to = duh;
            nodes[index].onclick = clickat;
            nodes[index].toBack();
            nodeList.add(nodes[index]);
            if(index < 2){
                nodes[index].load();
            }
        };
        return group;
}
public function addNode(node : CoverFlowNode){
            var index = nodeList.size();
            node.indexOfpositionerArray = index;
            node.ori_height = nodeH;
            node.ori_width = nodeW;
            node.offsetX = nodeW*scalrate + xoffset*index - overset;
            node.offsetY = yoffset;
            node.dw = dw;
            node.dlh = dlh;
            node.duh = duh;
            node.ori_height_to = nodeH;
            node.ori_width_to = nodeW;
            node.offsetx_to = xoffset*index;
            node.offsety_to = yoffset;
            node.dw_to = dw;
            node.dlh_to = dlh;
            node.duh_to = duh;
            node.onclick = clickat;
            node.toBack();
            nodeList.add(node);
            insert node into group.content;
}
public function clear(){
       for(node in nodeList){
            delete node as CoverFlowNode from group.content;
       }
       nodeList.clear();
       nodes = [];
}
}
